package cz.cvut.fit.mi.dpo.mvc.controller;

import cz.cvut.fit.mi.dpo.mvc.model.IWritable;
import cz.cvut.fit.mi.dpo.mvc.model.ZeroSizeException;
import cz.cvut.fit.mi.dpo.mvc.view.AView;
import cz.cvut.fit.mi.dpo.mvc.view.IMessageWriter;
import javax.swing.JOptionPane;

/**
 * @author David Pertl
 * @author Martin Štekl <steklmar@fit.cvut.cz>
 */
public class Controller {

    private IWritable model;
    private AView view;
    private IMessageWriter messageWriter;

    public Controller(IWritable model) {
        this.model = model;
    }

    public Controller setView(AView view) {
        this.view = view;
        return this;
    }

    public Controller setMessageWriter(IMessageWriter messageWriter) {
        this.messageWriter = messageWriter;
        return this;
    }

    public Controller addCircle(int x, int y) {
        try {
            model.createCircle(x, y);
        } catch (ZeroSizeException e) {
            messageWriter.showMessage(e.getMessage());
        }
        return this;
    }

    public Controller editCircle(int id, int x, int y, int r) {
        try {
            model.updateCircle(id, x, y, r);
        } catch (ZeroSizeException e) {
            messageWriter.showMessage(e.getMessage());
        }
        return this;
    }

    public Controller addSquare(int x, int y) {
        try {
            model.createSquare(x, y);
        } catch (ZeroSizeException e) {
            messageWriter.showMessage(e.getMessage());
        }
        return this;
    }

    public Controller editSquare(int id, int x, int y, int a) {
        try {
            model.updateSquare(id, x, y, a);
        } catch (ZeroSizeException e) {
            messageWriter.showMessage(e.getMessage());
        }
        return this;
    }

    public Controller clearAll() {
        model.clear();
        return this;
    }
    
    public boolean validateInteger(Object value) {
	    try {
		    Integer.parseInt(value.toString());
		    return true;
	    } catch (NumberFormatException nfe) {
		    messageWriter.showMessage("Only integer value is valid.");
	    }
        return false;
    }

}
